import java.util.ArrayList;
import java.util.Random;

/**
 * represents a random event in a mule game
 * @author Sherman
 *
 */
public class RandomEvent {
	private Random r = new Random();	//random variable used to determine if an event happens
	private String eventText;			//the message associated with an event
	
	private Event[] goodEvents = new Event[3];	//an array of good events
	private Event[] events = new Event[7];		//an array of all events
	private Event[] massEvents = new Event[3];
	
	/**
	 * creates an instance of a random event with a single player
	 * @param p the player the event is happening to
	 * @param roundNum the round the event is happening on
	 */
	public RandomEvent(Player p, int roundNum){
		
		goodEvents[0] = new Event(p, roundNum, "package");
		goodEvents[1] = new Event(p, roundNum, "repaid");
		goodEvents[2] = new Event(p, roundNum, "boughtPC");
		
		events[0] = new Event(p, roundNum, "package");
		events[1] = new Event(p, roundNum, "repaid");
		events[2] = new Event(p, roundNum, "boughtPC");
		events[3] = new Event(p, roundNum, "deadRat");
		events[4] = new Event(p, roundNum, "catBugs");
		events[5] = new Event(p, roundNum, "stolenFood");
		events[6] = new Event(p, roundNum, "gypsies");
			
		int eventNum = r.nextInt(100);
		
		//27% chance of event happening
		if (eventNum < 27){
			
			//check if last
			if (GameModel.getInstance().getLastPlace().equals(p)){
				eventText = goodEvents[r.nextInt(3)].occur();
			}
			else{
				eventText = events[r.nextInt(6)].occur();
			}
		}
		
		else{
			eventText = new Event(p, roundNum, "nothing").occur();
		}
	}

	/**
	 * creates an instance of a random event for all players
	 * @param pA the players the event is happening to
	 */
	public RandomEvent(ArrayList<Player> pA){
		
		massEvents[0] = new Event(pA, "mulePlague");
		massEvents[1] = new Event(pA, "earthquake");
		massEvents[2] = new Event(pA, "moneyStorm");
		
		int eventNum = r.nextInt(100);
		if (eventNum < 2){
			massEvents[r.nextInt(3)].occur();
		}
	}
	
	/**
	 * the message accompanying the event
	 * @return the event text
	 */
	public String getEventText(){
		return eventText;
	}
}
